package com.example.blog.event.describe;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.blog.domain.Comments;
import com.example.blog.dto.writing.WritingsDelDto;
import com.example.blog.event.publisher.WritingApplicationEvent;
import com.example.blog.service.impl.CommentsServiceImpl;
import com.example.blog.service.impl.WritingsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
@Async
public class WritingListen {
    private final WritingsServiceImpl writingsService;
    private final CommentsServiceImpl commentsService;
    @Autowired
    public WritingListen(WritingsServiceImpl writingsService,CommentsServiceImpl commentsService){
        this.writingsService = writingsService;
        this.commentsService = commentsService;
    }
    @EventListener
    public void writingsDel(WritingApplicationEvent writingApplicationEvent){
        WritingsDelDto writingsDelDto = (WritingsDelDto) writingApplicationEvent.getSource();
        QueryWrapper<Comments> wrapper = new QueryWrapper<>();
        commentsService.remove(wrapper.in("wid",writingsDelDto.getWid())); //删除文章评论
        writingsService.delImg(writingsDelDto.getFile()); //删除文章图片
    }
}
